// 封装的axios的公用方法 

import axios from "axios";
import { Message } from 'element-ui';
import qs from "qs";
import router  from "../router"
if(process.env.NODE_ENV == "development"){
	//axios.defaults.baseURL = "http://114.55.243.6:8080";
	axios.defaults.baseURL = "https://api.zhubaba.cn";
}else{
	//axios.defaults.baseURL = "http://114.55.243.6:8080";
	axios.defaults.baseURL = "https://api.zhubaba.cn";
}

// axios.defaults.timeout = 5000;
if(localStorage.getItem("key")){
    axios.defaults.headers.common['user-session'] = JSON.parse(localStorage.getItem("key"));
}
//增加COOKIES 
axios.defaults.withCredentials = true;

// 请求拦截器
axios.interceptors.request.use(function (config) {
    // if(config.method == "post"){
    //     config.data = qs.stringify(config.data);
    // }
    if(config.url == "/user/retrieve" || config.url == "/price" || config.url == "/user/sendMobileCode" || config.url == "/user/login" || config.url == "/user/register" || config.url == "/user/setInfo" || config.url == "/userTask/package" || config.url == "/user/logout"){
        config.headers["user-session"] = "";
    }else{
        config.headers["user-session"] = JSON.parse(localStorage.getItem("key"));
    }
    return config;
}, function (error) {
    // 对请求失败做处理
    return Promise.reject(error);
});
//响应拦截器
axios.interceptors.response.use(function (config) {
    
    if(config.data.code == 501){
        Message.error("登陆失效，即将跳转到登陆页");
        setTimeout(()=>{
            router.push("/userLogin")
        },1000)
    }else{
        return config;
    }
    // if(config.data.code == 0){
        
    // }else{
    //     Message.error(config.data.msg)
    // }
    
}, function (error) {
    // 对请求失败做处理
    console.log(error)
    Message.error("数据获取失败,请稍候再试!")
    return Promise.reject(error);
});
export default axios;